<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/21 0021
 * Time: 16:47
 */

namespace app\api\controller;


use app\service\AliyunOss;

class Upload extends Common
{
    /**
     * @name 上传图片
     * @author zay
     * @Date 2020/3/21 0021 16:47
     */
    public function image()
    {
        $userinfo = $this->userinfo;
        $appdata = $this->appdata;
        try {
            $file = request()->file('file');
            if (!$file) {
                throw new \Exception('图片不完整');
            }
            $type = isset($appdata['type']) ? $appdata['type'] : 1;
            /*if ($type == 2) {
                $filename = 'alipay';
                $ret = uploadimg($file, 'uploads/pay', $userinfo['uname'] . '_' . $filename, 2000);
            } else {
                $filename = date('YmdHis');
                $ret = uploadimg($file, 'uploads/tousu', $userinfo['uname'] . '_' . $filename, 2000);
            }*/
            if ($type == 2) {
                $filename = $userinfo['uname'] . '_alipay';
                $file_dir = 'uploads/pay';
            } else {
                $filename = $userinfo['uname'] . '_'.date('YmdHis');
                $file_dir = 'uploads/tousu';
            }
            $oss_data = [
                'file_dir'=>$file_dir,
                'file_name'=>$filename
            ];
            $ret = AliyunOss::service()->setParam($oss_data)->upload();
            if (!$ret['zt']) {
                apifasong(['zt' => '0', 'msg' => $ret['error']]);
            }
            $data['url'] = $ret['sql_path'];
            apifasong(['zt' => 1, 'msg' => '操作成功', 'data' => $data]);
        } catch (\Exception $e) {
            apifasong(['zt' => 0, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * @name 上传支付二维码
     * @author zay
     * @Date 2020/4/7 0007 17:44
     */

    public function code()
    {
        $userinfo = $this->userinfo;
        $appdata = $this->appdata;
        try {
            $file = request()->file('file');
            if (!$file) {
                throw new \Exception('图片不完整');
            }
            $filename = 'alipay';
            $ret = uploadimg($file, 'uploads/pay', $userinfo['uname'] . '_' . $filename, 2000);
            if (!$ret['zt']) {
                apifasong(['zt' => '0', 'msg' => $ret['error']]);
            }
            $data['url'] = $ret['sql_path'];
            apifasong(['zt' => 1, 'msg' => '操作成功', 'data' => $data]);
        } catch (\Exception $e) {
            apifasong(['zt' => 0, 'msg' => $e->getMessage()]);
        }
    }
}